/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio <spocchio@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package plar.elements;

import java.awt.Point;

import plar.core.Common;
import plar.core.Element;
import plar.core.Action;
import plar.core.Parameters;
import plar.core.Sprite;
import plar.core.defaultElements.Gun;
import plar.core.defaultElements.Flash;
import plar.core.defaultElements.Block;
import plar.core.defaultElements.Player;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import plar.core.Level;
import java.awt.Polygon;

public class GunRage extends Gun {
	
	public GunRage(Player player) {
super(player);
		 setProperty("gunName","Rage Gun");
		 setProperty("gunUseAmmo",10);
	}
	public void fire() {
		
		if (getAmmo() >= (Integer)getProperty("gunUseAmmo") ) {
		
			addAmmo(-(Integer)getProperty("gunUseAmmo") );
			setProperty("isFiring", true);
			Point shootSpeed = new Point();
			Integer s = (Integer) (getProperty("shootSpeed"));
			Double sdx = (Double) owner.getProperty("directionx");
			Double sdy = -(Double) owner.getProperty("directiony");
			
			Level  l = (Level) owner.getProperty("level");
			//Common.info("lowner :"+owner);
			//Common.info("level :"+l);
			//Common.info("Direction :["+sdx+","+sdy+"]");
			
			
			Polygon p = new Polygon();
			Point ShootPos = getShootRelativePosition();
			p.addPoint(ShootPos.x,ShootPos.y);
			Point second = new Point(ShootPos.x+((int)((double)sdx*10000)),ShootPos.y-((int)((double)sdy*10000)));
			p.addPoint(second.x, second.y);
			//Common.info("shootPos:"+ShootPos);
			//Common.info("Second:"+second);
			//Common.info("Line:"+p.getBounds());
			double minDist=-1;
			Element moreNear = null;
			for(Element e : l.elements)
			{
				if(p.intersects(e.getRectangle()) && e!=owner)
				{
					//Common.info("found:"+e);
					Rectangle pos = e.getRectangle();
					Point ebc = new Point(pos.x + pos.width/2, pos.y-pos.height/2);
					double a = -ShootPos.x+ebc.x;
					double b = -ShootPos.y+ebc.y;
					//a/=Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
					//b/=Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
					double dist = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
					if(minDist==-1 || dist<minDist)
					{
						minDist = dist;
						moreNear = e;
				///		Common.info("min dist:"+minDist+" is"+e+" "+moreNear.getProperty("user"));
					}
				}
			}
			//Common.info("min dist:"+minDist+" is"+moreNear.getProperty("user"));
			if(moreNear!=null)
			{
			//			Integer energy = (Integer) moreNear.getProperty("energy");
				//			moreNear.setProperty("energy", energy - 95);
					//		Common.info("shooted to:" + moreNear);
					
			Polygon p2 = new Polygon();
			p2.addPoint(ShootPos.x,ShootPos.y);
			p2.addPoint(moreNear.getPosition().x,moreNear.getPosition().y);
			Rectangle r = new Rectangle(p2.getBounds());
			Common.info("shoot:"+r);
		/*	Block b = new Block();
			b.setPosition(new Point(r.x,r.y));
			b.setSize(new Point(r.width,r.height));
			b.setStaticSprire("res/rage.png");
			b.actions.addAction("flash", new Action(){
				int i=5;
				public Parameters call(Parameters parameters)
				{
					Element me = (Element) parameters.get("me");
					String subAction = (String) parameters.get("subAction");
			Common.info("rage i:"+i);
			if (subAction == "main") {
				Common.info("rage i:"+i);
				if(i<=0)
				{
						parameters.put("subAction", "new");
						parameters.put("me", me);
						parameters.put("level", level);
						me.actions.runAction("destroy", parameters);
					}else i--;
					}
					return null;
			}
			
				}
			
			);
			b.setProperty("isRunnable", true);
			b.setProperty("canCollide", false);
			l.AddElement(b);
			*/
			Flash f = new Flash();
						int xd,yd;
			if(sdx>=0) xd= 1; else xd=-1;
			if(sdy>=0) yd= 1; else yd=-1;

			if(xd*yd<0)
			f.addSprite(new Sprite("res/ragebslash.png",50));
			else
			f.addSprite(new Sprite("res/rageslash.png",50));
			f.setPosition(new Point(r.x,r.y));
			f.setSize(new Point(r.width,r.height));
			f.Initialize();
			l.AddElement(f);
			Object miLiven = moreNear.getProperty("energy");
			if(miLiven!=null)
			{
							moreNear.setProperty("energy", ((Integer)miLiven) - 100);
							Common.info("shooted to:" + moreNear);
				
			}
			}
			
			
		}
	}
	


}
